<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class KBClientSearchModel extends KBClientModel
{	

	var $sql_params_group;
	
		
	function getEntriesSql($body_words = 70) {
		
		$sql = "
		SELECT 
			e.*,
			SUBSTRING_INDEX(e.body,' ',70) AS body, 
			cat.id AS category_id,
			cat.name AS category_name,
			cat.private AS category_private,			
			UNIX_TIMESTAMP(e.date_posted) AS ts_posted,
			UNIX_TIMESTAMP(e.date_updated) AS ts_updated,
			{$this->sql_params_select}
		
		FROM 
			({$this->tbl->entry} e, 
			{$this->tbl->category} cat,
			{$this->tbl->entry_to_category} e_to_cat
			{$this->sql_params_from})
			
		LEFT JOIN {$this->tbl->data_to_value} du ON e.id = du.data_value 
		                                        AND du.rule_id = '{$this->entry_member_role_rule}'
			
		WHERE 1
			AND e.id = e_to_cat.entry_id
			AND cat.id = e_to_cat.category_id
			AND e.active = 1
			AND cat.active = 1
			AND IF(du.user_value, du.user_value = '{$this->user_role_id}', 1)
			AND {$this->sql_params}
		{$this->sql_params_group}	
		{$this->sql_params_order}";
		
		//echo "<pre>"; print_r($sql); echo "</pre>";
		return $sql;
	}
	
	
	function &getEntryList($limit, $offset, $body_words = 100) {	

		$sql = $this->getEntriesSql($body_words);
		$result =& $this->db->SelectLimit($sql, $limit, $offset) or die(db_error($sql));
		return $result->GetArray();
	}	
}
?>